﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class CommentDAO
    {
        static DataClasses1DataContext Data = new DataClasses1DataContext();
        public static List<COMMENT> GetComment(int IDProduct)
        {
            try
            {
                var query = from Comment in Data.COMMENTs where Comment.IDPRODUCT == IDProduct select Comment;
                List<COMMENT> ListComment = new List<COMMENT>();
                foreach (COMMENT cm in query)
                    ListComment.Add(cm);
                return ListComment;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Insert(COMMENT Comment)
        {
            try
            {
                Comment.TIME_COMMENT = DateTime.Now;
                Data.COMMENTs.InsertOnSubmit(Comment);
                Data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Update(COMMENT Comment)
        {
            try
            {
                COMMENT comment = Data.COMMENTs.Single(com => com.IDCOMMENT == Comment.IDCOMMENT);
                comment.CONTENT = Comment.CONTENT;
                Data.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static int GetIDMax()
        {
            int maxID = Data.COMMENTs.Max(comment => comment.IDCOMMENT);
            return maxID;
        }
        public static bool IsExistingByProduct(int IDProduct)
        {
            try
            {
                bool isExisting = Data.COMMENTs.Any(Comment => Comment.IDPRODUCT == IDProduct);
                return isExisting;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool IsExisting(int IDComment)
        {
            try
            {
                bool isExisting = Data.COMMENTs.Any(Com=> Com.IDCOMMENT == IDComment);
                return isExisting;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static bool Delete(int idComment)
        {
            try
            {
                COMMENT Comment = Data.COMMENTs.Single(com => com.IDCOMMENT == idComment);
                Data.COMMENTs.DeleteOnSubmit(Comment);
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool DeleteByAccount(int idAccount)
        {
            try
            {
                var query = from comment in Data.COMMENTs where (comment.IDACCOUNT == idAccount) select comment;
                foreach (COMMENT Comment in query)
                    Data.COMMENTs.DeleteOnSubmit(Comment);
                Data.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
